#include "stallpage.h"
#include "ui_stallpage.h"
#include "widget.h"

stallpage::stallpage(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::stallpage)
{
    ui->setupUi(this);

    ui->tableWidget->setEditTriggers(QAbstractItemView::NoEditTriggers);
    ui->tableWidget->setSelectionBehavior(QAbstractItemView::SelectRows);  //单击选择行

    connect(ui->tableWidget,&QTableWidget::cellClicked,[&](int row,int ){
        ui->lineEdit_car->setText(ui->tableWidget->item(row,1)->text());
        ui->lineEdit_stall->setText(ui->tableWidget->item(row,0)->text());
    });
    Qurey();
}

stallpage::~stallpage()
{
    delete ui;
}


//识别后自动入库或者出库
void stallpage::insert_num(QString str){
    ui->lineEdit_car->setText(str);
    //查询stall数据库中该车牌号是否已经入库
    QSqlQuery query;
    query.prepare("select state from stall where carid =:value;");
    query.bindValue(":value",str);
    if(!query.exec()){
        qDebug() << "Error:select state faild for carid" << str;
    }
   query.next();
   if(1 == query.value(0)){
        //该车已经在停车场中,执行出库操作
        Out_stall(str);
        QMessageBox::information(this,"车辆消息","检测到有车辆出库！");

   }else{
        //该车未在停车场中,执行入库操作
        In_stall(str);
        emit parking_fee(0);
        QMessageBox::information(this,"车辆消息","检测到有车辆入库！");
   }
   Qurey();

}

//查询并显示数据库
void stallpage::Qurey()
{
    int row = 0;
    ui->tableWidget->setRowCount(0);

    QSqlQuery query;
    if(query.exec("SELECT stallid,carid FROM stall"))
    {
        QSqlRecord record = query.record();
        //ui->tableWidget->setColumnCount(record.count()-5);
        ui->tableWidget->setColumnCount(2);
        ui->tableWidget->setHorizontalHeaderLabels({"车位号","车牌号"});//,"收费"

        while(query.next())
        {
            ui->tableWidget->insertRow(row);
            for(int var = 0;var<record.count();++var)
            {
                ui->tableWidget->setItem(row,var,new QTableWidgetItem(query.value(var).toString()));
            }
            row++;
        }
    }
    else
    {
        qDebug() << "查询失败：" << query.lastError().text();
    }

    if (!query.exec("UPDATE stall SET state = 0 WHERE state = 1 AND carid IS NULL;")) {
            // 如果有错误，打印错误消息
            qDebug() << "更新失败：" << query.lastError().text();
            return ;
        }

}
//入库
void stallpage::In_stall(QString carid)
{

    //获取当前的时间
    QDateTime currentTime = QDateTime::currentDateTime();
    // 格式化时间字符串（例如：yyyy-MM-dd HH:mm:ss）
    QString formattedTime = currentTime.toString("yyyy-MM-dd HH:mm:ss");
    //找空车位
    QSqlQuery query;
    query.prepare("SELECT stallid from stall WHERE state = 0;");
    if(!query.exec())
    {
        qDebug() << "SELECT stallid from stall WHERE state = 0";
        return;
    }
    query.next();
    int stallId = query.value(0).toInt();
    QSqlQuery updateQuery;
    updateQuery.prepare("UPDATE stall SET carid = :carId, in_time = :Time, state = 1 WHERE stallid = :stallId;");
    updateQuery.bindValue(":carId", carid);
    updateQuery.bindValue(":Time", formattedTime);
    updateQuery.bindValue(":stallId", stallId);

    // 执行更新操作
    if (!updateQuery.exec()) {
        qDebug() << "Error: Failed to update stall with stallid =" << stallId;
    } else {
        qDebug() << "Success: Updated stall with stallid =" << stallId;
    }


}
//出库
void stallpage::Out_stall(QString carid)
{
//更新出库时间
       QSqlQuery updateQuery;
       //获取当前的时间
       QDateTime currentTime = QDateTime::currentDateTime();
       // 格式化时间字符串（例如：yyyy-MM-dd HH:mm:ss）
       QString formattedTime = currentTime.toString("yyyy-MM-dd HH:mm:ss");
       updateQuery.prepare("UPDATE stall SET out_time = :Time WHERE carid = :carid");
       updateQuery.bindValue(":carid", carid);
       updateQuery.bindValue(":Time", formattedTime);
       // 执行更新操作
       if (!updateQuery.exec()) {
           qDebug() << "Error0: failed to update carid for stallid";
       }
       qDebug() << "出库时间：" << formattedTime;

//查询是不是VIP用户
       QSqlQuery checkquery;
       checkquery.prepare("SELECT stallid from user WHERE carid=:carid");
       checkquery.bindValue(":carid",carid);
       if(!checkquery.exec())
       {
           qDebug() << "Check if user is a VIP member  Error";
           return;
       }
       checkquery.next();
       double fee = 0;//费用
       double hourlyRate = 5.0; // 每小时5元
       int freeMinutes = 15; // 前15分钟免费
       double dailyMax = 50.0; // 每天最多50元
       int stallId = checkquery.value(0).toInt();
 //计算费用
       if(stallId){
           qDebug() << "该车牌为vip :" << carid;
           fee = -1;
       }else{
           QString In_time= selectin(carid);
           QString Out_time = selectout(carid);
           fee = calculateParkingFee(In_time, Out_time, hourlyRate, freeMinutes, dailyMax);
           qDebug() << "本次费用：" <<fee;
        }
//将 stall 表中的cost更新为fee
        updateQuery.prepare("UPDATE stall SET cost = :fee WHERE carid = :carid");
        updateQuery.bindValue(":fee", fee);
        updateQuery.bindValue(":carid", carid);

        if (!updateQuery.exec()) {
            qDebug() << "Error: failed to stall SET cost = :fee for carid" << carid;
        } else {
            qDebug() << "Successfully stall SET cost = :fee for carid" << carid;
        }
        QSqlQuery insertQuery;
// 将 stall 表中的carid,stallid,in_time,out_time,cost数据插入到 access 表中carid, stallid, time_in, time_out,cost
        insertQuery.prepare("INSERT INTO access (carid, stallid, time_in, time_out,cost)  SELECT carid, stallid, in_time, out_time,cost FROM stall WHERE carid = :carid;");

        insertQuery.bindValue(":carid", carid);
        if (!insertQuery.exec()) {
            qDebug() << "Error: failed to insert data from stall to access for carid" << carid;
            emit parking_fee(fee);
            return;
        } else {
            qDebug() << "Successfully inserted data from stall to access for carid" << carid;
        }
        QSqlQuery query;
        query.prepare("SELECT out_time FROM stall WHERE carid=:carid");
        query.bindValue(":carid", carid);

        if (!query.exec()) {
            qDebug() << "Error executing query:" << query.lastError();
            return;
        }

        // 检查是否有结果返回
        if (query.first()) { // 移动到第一条记录
            do {

                // 获取当前行的out_time字段值
                 QString outTime = query.value("out_time").toString();

                // 打印out_time
                qDebug() << "Out Time:" << outTime;

            } while (query.next()); // 继续移动到下一条记录
        }

//删除stall中的本次停车记录
        QSqlQuery deleteQuery;
        deleteQuery.prepare("UPDATE stall SET carid = NULL, in_time = NULL, state = 0 WHERE carid = :carid;");
        deleteQuery.bindValue(":carid", carid);
        // 执行删除操作
        if (!deleteQuery.exec()) {
            qDebug() << "Error: failed to delete from stall WHERE carid =" << carid;
        } else {
            qDebug() << "Successfully deleted from stall WHERE carid =" << carid;
        }
        emit parking_fee(fee);
}



//手动添加入库
void stallpage::on_btn_add_clicked()
{
    int stallId1;
    QString carIdValue = ui->lineEdit_car->text();

    QSqlQuery userquery;
    userquery.prepare("SELECT stallid from user WHERE carid=:carId");
    userquery.bindValue(":carId",carIdValue);
    if(!userquery.exec())
    {
        qDebug() << "failed to SELECT stallid from user WHERE carid=:carId";
        return;
    }
    userquery.next();
    stallId1 = userquery.value(0).toInt();
    if(stallId1){
    qDebug() << "该车牌为vip" <<QString::number(stallId1);
    //QString stallId = stallId1
    ui->lineEdit_stall->setText(QString::number(stallId1));
    }

    stallId1=ui->lineEdit_stall->text().toInt();
    if(stallId1)//输入车牌号入库的情况
    {
        QString carIdValue = ui->lineEdit_car->text();
        //检查输入的carid和车位号是否已经录入或使用
        if(hasDuplicateCarids(carIdValue)){
            QMessageBox::information(this,"录入错误","该车牌已录入！");
            return;
        }
        QSqlQuery cheakQuery;
        cheakQuery.prepare("SELECT state FROM stall WHERE stallid=:stallId");
        cheakQuery.bindValue(":stallId",stallId1);
        if (!cheakQuery.exec()) {
            qDebug() << "Error0: failed to update carid for stallid" << stallId1;
        }
        cheakQuery.next();
        if(1 == cheakQuery.value(0).toInt()){
            QMessageBox::information(this,"录入错误","该车位已录入车牌！");
            return;
        }

        QSqlQuery updateQuery;
        updateQuery.prepare("UPDATE stall SET carid = :carId WHERE stallid = :stallId");
        updateQuery.bindValue(":carId", carIdValue);
        updateQuery.bindValue(":stallId", stallId1);
        // 执行更新操作
        if (!updateQuery.exec()) {
            qDebug() << "Error0: failed to update carid for stallid" << stallId1;
        } else {
            qDebug() << "Successfully updated carid for stallid" << stallId1;
            Qurey();
        }

        //更新时间
        updateQuery.prepare("UPDATE stall SET in_time = :Time WHERE stallid = :stallId");
        updateQuery.bindValue(":stallId", stallId1);
        //获取当前的时间
        QDateTime currentTime = QDateTime::currentDateTime();
        // 格式化时间字符串（例如：yyyy-MM-dd HH:mm:ss）
        QString formattedTime = currentTime.toString("yyyy-MM-dd HH:mm:ss");
        updateQuery.bindValue(":Time", formattedTime);
        // 执行更新操作
        if (!updateQuery.exec()) {
            qDebug() << "Error0: failed to update TIME for stallid" << stallId1;
        } else {
            qDebug() << "Successfully updated TIME for stallid" << stallId1;
            Qurey();
        }

        QSqlQuery stateQuery;
        stateQuery.prepare("UPDATE stall SET state = 1 WHERE stallid = :stallId");
        stateQuery.bindValue(":stallId", stallId1);
        if (!stateQuery.exec()) {
            qDebug() << "Error1: failed to update carid for state" << stallId1;
        } else {
            qDebug() << "Successfully updated carid for state" << stallId1;
            Qurey();
        }
        qDebug() << stallId1;
    }
    else{
        if(!add_nonstall())
            return;//分配车位号入库的情况
    }
     qDebug()<< "入库程序已完成";

    emit parking_fee(0);

}





//出库
void stallpage::on_btn_del_clicked()
{
    int stallId2 = select1(ui->lineEdit_car->text());
    QString caridValue = ui->lineEdit_car->text();
    QSqlQuery updateQuery;

    updateQuery.prepare("UPDATE stall SET out_time = :Time WHERE stallid = :stallId");
    updateQuery.bindValue(":stallId", stallId2);

    //获取当前的时间
    QDateTime currentTime = QDateTime::currentDateTime();
    // 格式化时间字符串（例如：yyyy-MM-dd HH:mm:ss）
    QString formattedTime = currentTime.toString("yyyy-MM-dd HH:mm:ss");
    updateQuery.bindValue(":Time", formattedTime);
    // 执行更新操作
    if (!updateQuery.exec()) {
        qDebug() << "Error0: failed to update carid for stallid" << stallId2;
    } else {
        qDebug() << "Successfully updated carid for stallid" << stallId2;
        Qurey();
    }

    QSqlQuery insertQuery;
    QString In_time= selectin(caridValue);
    QString Out_time = selectout(caridValue);
    qDebug() <<In_time;
    qDebug() <<Out_time;
    double hourlyRate = 5.0; // 每小时5元
    int freeMinutes = 15; // 前15分钟免费
    double dailyMax = 50.0; // 每天最多50元
    double fee = calculateParkingFee(In_time, Out_time, hourlyRate, freeMinutes, dailyMax);
    qDebug() <<fee;

    //如果user中有该carid则不收费
    QSqlQuery userquery;
    userquery.prepare("SELECT stallid from user WHERE carid=:carId");
    userquery.bindValue(":carId",caridValue);
    if(!userquery.exec())
    {
        qDebug() << "failed to SELECT stallid from user WHERE carid=:carId";
        return;
    }
    userquery.next();
    if(int stallId = userquery.value(0).toInt()){
    qDebug() << "该车牌为vip" <<QString::number(stallId);
    fee = 0;
    ui->lineEdit_stall->setText(QString::number(stallId));
    }

    //将 stall 表中的cost更新为fee
    updateQuery.prepare("UPDATE stall SET cost = :fee WHERE stallid = :stallId");
    updateQuery.bindValue(":fee", fee);
    updateQuery.bindValue(":stallId", stallId2);

    if (!updateQuery.exec()) {
        qDebug() << "Error: failed to stall SET cost = :fee for stallid2" << stallId2;
    } else {
        qDebug() << "Successfully stall SET cost = :fee for stallid2" << stallId2;
    }

    // 将 stall 表中的carid,stallid,in_time,out_time数据插入到 access 表中carid, stallid, time_in, time_out
    insertQuery.prepare("INSERT INTO access (carid, stallid, time_in, time_out,cost)  SELECT carid, stallid, in_time, out_time,cost FROM stall WHERE stallid = :stallId;");

    insertQuery.bindValue(":stallId", stallId2);
    if (!insertQuery.exec()) {
        qDebug() << "Error: failed to insert data from stall to access for stallid" << stallId2;
    } else {
        qDebug() << "Successfully inserted data from stall to access for stallid" << stallId2;
    }

    //删除车牌号
    updateQuery.prepare("UPDATE stall SET carid = NULL WHERE stallid = :stallId");
    updateQuery.bindValue(":stallId", stallId2);
    // 执行更新操作
    if (!updateQuery.exec()) {
        qDebug() << "Error: failed to stall SET carid = NULL WHERE stallid = :stallId for stallid2" << stallId2;
    } else {
        qDebug() << "Successfully stall SET carid = NULL WHERE stallid = :stallId for stallid2" << stallId2;
        Qurey();
    }



    QSqlQuery stateQuery;
    stateQuery.prepare("UPDATE stall SET state = 0 WHERE stallid = :stallId");
    stateQuery.bindValue(":stallId", stallId2);
    if (!stateQuery.exec()) {
        qDebug() << "Error: failed to update carid for state" << stallId2;
    } else {
        qDebug() << "Successfully updated carid for state" << stallId2;
        QString message = QString("出库成功，%1 收费 %2元 ！").arg(caridValue).arg(fee);
        QMessageBox::information(this,"出库成功",message);
        Qurey();
    }
    emit parking_fee(fee);

    qDebug() << stallId2;

}


//添加车位
void stallpage::on_btn_addstall_clicked()
{
    int stallId = ui->lineEdit_stall->text().toInt();
    if(stallId )
    {
        QSqlQuery insertQuery;
        insertQuery.prepare("INSERT INTO stall(stallid,state) VALUES(:stallId,0) ");
        insertQuery.bindValue(":stallId", stallId);
        if (!insertQuery.exec()) {
            qDebug() << "Error: failed to add stall" << stallId;
        } else {
            qDebug() << "Successfully add stall" << stallId;
        }
        qDebug() << stallId;
    }else{
        QSqlQuery query;
        query.prepare("SELECT stallid FROM stall");
        if (!query.exec()) {
            qDebug() << "Error: failed to execute query for finding duiyinde carid records";
            return;
        }
        // 遍历所有找到的记录，并为它们设置carID值
        while (query.next()) {
            stallId = query.value(0).toInt(); // 获取stallid
        }
        qDebug() << stallId;
        stallId++;
        QSqlQuery insertQuery;
        insertQuery.prepare("INSERT INTO stall(stallid,state) VALUES(:stallId,0) ");
        insertQuery.bindValue(":stallId", stallId);
        if (!insertQuery.exec()) {
            qDebug() << "Error: failed to add stall" << stallId;
        } else {
            qDebug() << "Successfully add stall" << stallId;
        }
    }
    ui->lineEdit_stall->setText("");
    Qurey();

}
//删除车位
void stallpage::on_bth_delstall_clicked()
{
    int stallId = ui->lineEdit_stall->text().toInt();
    //确定carid为NULL再删除
    QSqlQuery query;
    query.prepare("SELECT carid FROM stall WHERE stallid = :stallId");
    query.bindValue(":stallId",stallId);
        if (!query.exec()) {
            qDebug() << "Error: failed to SELECT carid FROM stall WHERE stallid = :stallId";
        }
    query.next();
    if(!query.value(0).toBool())
    {
        QSqlQuery insertQuery;
        insertQuery.prepare("DELETE FROM stall WHERE stallid = :stallId;");
        insertQuery.bindValue(":stallId", stallId);
        if (!insertQuery.exec()) {
            qDebug() << "Error: failed to delete stall" << stallId;
        } else {
            qDebug() << "Successfully delete stall" << stallId;
        }
        qDebug() << stallId;
    }else{
    QMessageBox::information(this,"删除错误","该车位已录车牌！");
    }
        ui->lineEdit_stall->setText("");
    Qurey();

}


//检索车牌号，返回车位号
int stallpage::select1(QString carIdValue)
{
    QSqlQuery query;
    query.prepare("SELECT stallid FROM stall WHERE carid = :carid");
    query.bindValue(":carid",carIdValue);
    if (!query.exec()) {
        qDebug() << "Error: failed to execute query for finding duiyinde carid records";
        return -1;
    }
    int stallId = -1;
    // 遍历所有找到的记录，并为它们设置carID值
    while (query.next()) {
        stallId = query.value(0).toInt(); // 获取stallid
    }
    return stallId;
}
//检索车牌号，返回入库时间
QString stallpage::selectin(QString carIdValue)
{
    QSqlQuery query;
    query.prepare("SELECT in_time FROM stall WHERE carid = :carid");
    query.bindValue(":carid",carIdValue);
    if (!query.exec()) {
        qDebug() << "Error: failed to execute query for finding duiyinde carid records";
        return "error in ";
    }
    QString In_time;
    // 遍历所有找到的记录，并为它们设置carID值
    while (query.next()) {
        In_time = query.value(0).toString(); // 获取stallid
    }
    return In_time;

}
//检索车牌号，返回出库时间
QString stallpage::selectout(QString carIdValue)
{
    QSqlQuery query;
    query.prepare("SELECT out_time FROM stall WHERE carid = :carid");
    query.bindValue(":carid",carIdValue);
    if (!query.exec()) {
        qDebug() << "Error: failed to execute query for finding duiyinde carid records";
        return "error out";
    }
    QString Out_time;
    // 遍历所有找到的记录，并为它们设置carID值
    while (query.next()) {
        Out_time = query.value(0).toString(); // 获取stallid
    }
    return Out_time;
}


// 将时间字符串转换为time_t
time_t stallpage::stringToTimeT(QString timeStr)
{
    std::tm tm = {};
    std::istringstream ss(timeStr.toStdString());
    ss >> std::get_time(&tm, "%Y-%m-%d %H:%M:%S");
    return std::mktime(&tm);

}


// 计算停车费用
double stallpage::calculateParkingFee(QString In_time, QString Out_time, double hourlyRate, int freeMinutes, double dailyMax)
{
    time_t entryTime = stringToTimeT(In_time);
    time_t exitTime = stringToTimeT(Out_time);
    if (exitTime <= entryTime) {
            return 0.0; // 无效时间，返回0元
        }

        // 计算停车总秒数
        int parkingDuration = difftime(exitTime, entryTime);

        // 转换为小时和分钟
        int totalHours = static_cast<int>(parkingDuration / 3600);
        int remainingMinutes = static_cast<int>((parkingDuration % 3600) / 60);

        // 减去免费时间（分钟）
        int paidMinutes = (totalHours * 60 + remainingMinutes) - freeMinutes;
        if (paidMinutes < 0) paidMinutes = 0; // 如果免费时间足够长，则不需要付费

        // 计算总小时数（包括不足一小时的部分）
        int totalPaidHours = paidMinutes / 60 + (paidMinutes % 60 > 0 ? 1 : 0);

        // 计算天数和余数小时
        int totalDays = totalPaidHours / 24;
        int remainingHours = totalPaidHours % 24;

        // 计算费用
        double fee = 0.0;
        for (int i = 0; i < totalDays; ++i) {
            fee += dailyMax; // 每天的封顶费用
        }
        // 加上余数小时的费用（但不超过当天的封顶费用）
        double remainderFee = static_cast<double>(remainingHours) * hourlyRate;
        fee += std::min(remainderFee, dailyMax - (totalDays > 0 ? 0.0 : static_cast<int>(remainderFee) % static_cast<int>(dailyMax)));

        return fee;
}

//是否有重复车牌号
bool stallpage::hasDuplicateCarids(const QString &carid)
{
    QSqlQuery searchquery;
        // 修改查询以检查特定carid的重复
        // 我们可以直接使用COUNT(*)与WHERE子句来判断是否有多于一行的结果
        QString queryStr = "SELECT COUNT(*) FROM stall WHERE carid = :carid";

        // 准备查询并绑定参数
        if (!searchquery.prepare(queryStr)) {
            qDebug() << "查询准备失败：" << searchquery.lastError().text();
            return false; // 假设在准备失败时认为没有重复项
        }
          qDebug() <<carid;
        searchquery.bindValue(":carid", carid);

        // 执行查询
        if (!searchquery.exec()) {
            qDebug() << "查询执行失败：" << searchquery.lastError().text();
            return false; // 假设在执行失败时认为没有重复项
        }

        // 如果查询结果集的第一行表示计数大于1，则返回true
        // 注意：由于我们只对计数感兴趣，我们不需要调用searchquery.next()多次

        if (searchquery.next() && searchquery.value(0).toInt() >= 1) {
            return true;
        }

        // 如果没有找到重复项（即计数不大于1），则返回false
        return false;
}

//向stall表插入carid
int stallpage::insert_carid(QString carIdValue)
{
    QSqlQuery query;
    // 查找所有state为0的记录
        query.prepare("SELECT COUNT(*) FROM stall WHERE state = 0;");

        if (!query.exec()) {
            qDebug() << "Error: SQL query failed: " << query.lastError().text();
            return -1;
        }
        int count1 = -1;
        // 获取查询结果
        if (query.next()) {
            count1 = query.value(0).toInt();
            //qDebug() << "Number of stalls with state 0:" << count1;
        }else{
            return -1;
        }
        if(count1 == 0)
            return -1;
        if(count1 == -1)
            return -1;
    query.prepare("SELECT stallid FROM stall WHERE state = 0");
    if (!query.exec()) {
        qDebug() << "Error: failed to execute query for finding NULL carid records";
        return -1;
    }
    int stallId1 = 0;
    // 遍历所有找到的记录，并为它们设置carID值
    while (query.next()) {
        stallId1 = query.value(0).toInt();
        if(stallId1)// 获取stallid
        break;
    }
    // 准备更新语句
    QSqlQuery updateQuery;
    updateQuery.prepare("UPDATE stall SET carid = :carId WHERE stallid = :stallId");
    updateQuery.bindValue(":carId", carIdValue);
    updateQuery.bindValue(":stallId", stallId1);
    // 执行更新操作
    if (!updateQuery.exec()) {
        qDebug() << "Error0: failed to update carid for stallid" << stallId1;
    } else {
        qDebug() << "Successfully updated carid for stallid" << stallId1;
        Qurey();
    }
    return stallId1;
}


//没有输入车位号情况下，使用的入库方法
int stallpage::add_nonstall()
{
    QString carId = ui->lineEdit_car->text(); // 这是您想插入的carID值
    if(carId.isEmpty()){
        qDebug()<<"该车牌号为空";
        return 0;
    }
    if (hasDuplicateCarids(carId)) {
       qDebug() << "表stall中存在重复的carid项。";
       QMessageBox::information(this,"录入错误","该车牌已录入！");
       return 0;
   } else {
       qDebug() << "表stall中没有重复的carid项。";
   }
    int stallId1 = insert_carid(ui->lineEdit_car->text());//插入车牌并返回对应的stallid值

    //更新时间
    QSqlQuery updateQuery;
    updateQuery.prepare("UPDATE stall SET in_time = :Time WHERE stallid = :stallId");
    updateQuery.bindValue(":stallId", stallId1);
    //获取当前的时间
    QDateTime currentTime = QDateTime::currentDateTime();
    // 格式化时间字符串（例如：yyyy-MM-dd HH:mm:ss）
    QString formattedTime = currentTime.toString("yyyy-MM-dd HH:mm:ss");
    updateQuery.bindValue(":Time", formattedTime);
    // 执行更新操作
    if (!updateQuery.exec()) {
        qDebug() << "Error0: failed to update TIME for stallid" << stallId1;
    } else {
        qDebug() << "Successfully updated TIME for stallid" << stallId1;
        Qurey();
    }
    //设置车位状态
    QSqlQuery stateQuery;
    stateQuery.prepare("UPDATE stall SET state = 1 WHERE stallid = :stallId");
    stateQuery.bindValue(":stallId", stallId1);
    if (!stateQuery.exec()) {
        qDebug() << "Error1: failed to update carid for state" << stallId1;
    } else {
        qDebug() << "Successfully updated carid for state" << stallId1;
        Qurey();
    }
    qDebug() << stallId1;
    return 1;

}
